package com.example.mystoryapp2.ui.story.storylist

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.example.mystoryapp2.R
import com.example.mystoryapp2.contexts.StoryContext
import com.example.mystoryapp2.databinding.ActivityBaseStoryListLayoutBinding
import com.example.mystoryapp2.domain.Story
import com.example.mystoryapp2.enums.EnumListMode
import com.example.mystoryapp2.ui.base.BaseActivity
import com.example.mystoryapp2.ui.base.BaseListAdapter
import com.example.mystoryapp2.ui.story.StoryDescriptionActivity
import com.example.mystoryapp2.ui.utils.RecycleViewUtils

class StoryListActivity : BaseActivity<ActivityBaseStoryListLayoutBinding,
        StoryListViewModel>() {

    private val TAG = "PublishedStoryActivity"

    companion object{
        fun startPublished(ctx: Context){
            StoryContext.listMode = EnumListMode.WORK
            StoryContext.storyIsPublished = true
            startActivity(ctx)
        }

        fun startUnPublished(ctx: Context){
            StoryContext.listMode = EnumListMode.WORK
            StoryContext.storyIsPublished = false
            startActivity(ctx)
        }

        fun startCollected(ctx: Context){
            StoryContext.listMode = EnumListMode.COLLECTED
            startActivity(ctx)
        }

        private fun startActivity(ctx: Context){
            val intent = Intent(ctx, StoryListActivity::class.java)
            ctx.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        showBackTitle()
        initData()
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
        initStoryList()
    }

    protected fun toStory(s: Story) {
        StoryContext.storyId = s.getStoryId()!!
        var intent = Intent(
            this@StoryListActivity,
            StoryDescriptionActivity::class.java
        )
        startActivity(intent)
    }

    private fun initData() {

        initStoryList()
    }

    private fun initStoryList() {
//        val layoutManager = LinearLayoutManager(
//            this, LinearLayoutManager.VERTICAL,
//            false
//        )
//        db.rv.layoutManager = layoutManager
        RecycleViewUtils.addLLLayoutManager(this@StoryListActivity,db.rv)
        db.viewModel?.initStoryList(
            object : BaseListAdapter.IClickListener<Story> {
                override fun onClick(view: View, item: Story) {
                    when (view.id) {
                        R.id.llItem, R.id.btnImg, R.id.tvDescription, R.id.tvTitle -> {
                            Log.d(TAG, "onClick: $item")
                            toStory(item)
                        }
                    }
                }
            })
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_base_story_list_layout
    }

    override fun getViewModel(): StoryListViewModel {
        return ViewModelProvider(
            this, ViewModelProvider
                .AndroidViewModelFactory(application)
        )[StoryListViewModel::class.java]
    }

    override fun setViewModel() {
        db.viewModel = getViewModel()
    }
}